package com.chaosj.memory.handlers;

import com.chaosj.memory.dto.SchulteGridDto;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * @className: SchulteGridHandler
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/10
 **/
public class SchulteGridHandler extends AbstractToolHandler<SchulteGridDto> {
    @Override
    public String name() {
        return "srt";
    }

    @Override
    public void handler(SchulteGridDto req, StringBuffer outBuffer) {
        Set<Integer> has = new HashSet<>();
        for( int i = 0; i < req.getSize(); i++ ){
            outBuffer.append("*                   ");
            for ( int j = 0; j < req.getSize(); j++ ){
                int rmdInteger = 0;
                do{
                    rmdInteger = next.nextInt(req.getSize()*req.getSize()+1 );
                }while (has.contains(rmdInteger)||rmdInteger==0);
                has.add(rmdInteger);
                outBuffer.append("|"+(rmdInteger<10?"0"+rmdInteger:rmdInteger));
            }
            outBuffer.append("|                  *\n");
        }
    }

    @Override
    public SchulteGridDto cmd2Dto(String[] args) {
        return new SchulteGridDto(args);
    }
}
